package stu.aistar.controller;
import com.alipay.api.AlipayApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import stu.aistar.config.AlipayConfig;
import stu.aistar.dao.IOrdersDao;
import stu.aistar.model.pojo.Orders;
import stu.aistar.model.vo.PayVo;
import stu.aistar.model.vo.SeatVo;
import stu.aistar.service.IOrdersService;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    private SeatVo seatVo;
    private Orders orders;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrdersDao ordersDao;
    @Autowired
    private IOrdersService ordersService;

    @RequestMapping(value = "payorder",produces = "text/html")
    @ResponseBody
    public String payorder(){
        PayVo payVo=new PayVo();
        payVo.setBody(orders.getSeats());
        payVo.setOut_trade_no(orders.getOrdersNo());
        payVo.setPrice(BigDecimal.valueOf(orders.getPrice()));
        payVo.setSubject(orders.getMovieName());
        try {
            String result=AlipayConfig.pay(payVo);
            System.out.println(result);
            return result;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
       return "";
    }
    @GetMapping("seatsinfo")
    @ResponseBody
    public String seatsInfo(SeatVo seatVo1){
        redisTemplate.opsForValue().set("str",seatVo1);
        System.out.println(seatVo1);
        seatVo=seatVo1;
        return "success";
    }
    @GetMapping("/createorder")
    public String createOrder(){
        String sno = String.format("%tF",new Date());
        String sno2 = String.format("%tT",new Date());
        String order_no = sno.replace("-","")+sno2.replace(":","");
       Double total=seatVo.getTotal();
       Double price=seatVo.getPrice();
       Date time=new Date();
       Integer user_id=1;
       String movie_name=seatVo.getMovieName();
       String seats=seatVo.getSeatArea();
       orders=new Orders(null,order_no,price,total,time,1,user_id,movie_name,seats,null);
        Integer result=ordersService.insertOrders(orders);
//        return "404";
        return "redirect:/order/payorder";
    }

    @GetMapping("/userorder")
    public String plan(Model model, Integer userId,Integer sort){
        List<Orders> ordersList=ordersDao.findOrdersByUserId(userId);
        if(sort==1){
         ordersList.sort(((o1, o2) -> (int) (o2.getOrdersNo().compareTo(o1.getOrdersNo()))));
        }else{
            ordersList.sort(((o1, o2) -> (int) (o1.getOrdersNo().compareTo(o2.getOrdersNo()))));
        }

        model.addAttribute("orderList",ordersList);
        model.addAttribute("userId",userId);
        return "user-orders";
    }
    @GetMapping("/success")
    public String playsuccess(){
        //return "/WEB-INF/404.jsp";
        return "play-success";
    }

    @GetMapping("/error")
    public String playerror(){
        //return "/WEB-INF/404.jsp";
        return "404";
    }

}
